package com.taxt.ui.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taxt.R;
import com.taxt.aop.SingleClick;
import com.taxt.app.TitleBarFragment;
import com.taxt.http.api.LoginApi;
import com.taxt.http.model.PersonInfoBean;
import com.taxt.other.SpUtils;
import com.taxt.other.ToolsUtils;
import com.taxt.ui.activity.HomeActivity;
import com.taxt.ui.activity.LoginActivity;
import com.taxt.ui.activity.PersonalBaseDataActivity;
import com.taxt.ui.activity.PersonalDeclareDataActivity;
import com.taxt.ui.activity.PersonalJobDataActivity;

/**
 * author : wq
 * <p>
 * time   : 2018/10/18
 * desc   : 我的 Fragment
 */
public final class MineFragment extends TitleBarFragment<HomeActivity> {

    public static MineFragment newInstance() {
        return new MineFragment();
    }

    private TextView mUserInfo, mUserPhone, mExitBtn;
    private ImageView mUserHead;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment;
    }

    @Override
    protected void initView() {
        mUserInfo = findViewById(R.id.user_info);
        mUserPhone = findViewById(R.id.user_phone);
        mUserHead = findViewById(R.id.user_head);
        mExitBtn = findViewById(R.id.exit_btn);
        setOnClickListener(R.id.sb_setting_info, R.id.sc_setting_job_info, R.id.user_info, R.id.exit_btn);
    }

    @Override
    protected void initData() {

    }

    @SingleClick
    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        if (viewId == R.id.sb_setting_info) {
            if (!(SpUtils.decodeString("token") == null || SpUtils.decodeString("token").equals(""))) {
                startActivity(PersonalBaseDataActivity.class);
            } else {
                startActivity(LoginActivity.class);
            }

        } else if (viewId == R.id.sc_setting_job_info) {
            if (!(SpUtils.decodeString("token") == null || SpUtils.decodeString("token").equals(""))) {
                startActivity(PersonalJobDataActivity.class);
            } else {
                startActivity(LoginActivity.class);
            }
        } else if (viewId == R.id.user_info) {
            if (!(SpUtils.decodeString("token") == null || SpUtils.decodeString("token").equals(""))) {

            } else{
                startActivity(LoginActivity.class);
        }
    }else if(viewId==R.id.exit_btn)

    {
        showUserInfo(2);
    }

}

    @Override
    public boolean isStatusBarEnabled() {
        // 使用沉浸式状态栏
        return super.isStatusBarEnabled();
    }

    @Override
    protected boolean isStatusBarDarkFont() {
        return false;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!(SpUtils.decodeString("token") == null || SpUtils.decodeString("token").equals(""))) {
            showUserInfo(1);
        } else {
            showUserInfo(2);
        }
    }

    public void showUserInfo(int flag) {
        if (flag == 1) {
            mUserPhone.setVisibility(View.VISIBLE);
            mExitBtn.setVisibility(View.VISIBLE);
            PersonInfoBean personInfoBean = (PersonInfoBean) SpUtils.decodeParcelable("person", PersonInfoBean.class);
            if (personInfoBean != null) {
                PersonInfoBean.PersonInfoDTO bean = personInfoBean.getPersonInfo();
                if (bean != null) {
                    mUserInfo.setText(bean.getName());
                    mUserPhone.setText(ToolsUtils.getStarMobile(bean.getPhoneNumber()));
                    if (bean.getSex().equals("女")) {
                        mUserHead.setImageResource(R.mipmap.nv);
                    } else {
                        mUserHead.setImageResource(R.mipmap.nan);
                    }
                }
            }
        } else {
            SpUtils.encode("token", "");
            SpUtils.encodeParcelable("home",null);
            mExitBtn.setVisibility(View.GONE);
            mUserInfo.setText("登录/注册");
            mUserHead.setImageResource(R.mipmap.login_avatar_default);
            mUserPhone.setVisibility(View.GONE);
        }
    }
}